﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using QLKetNoi;
using System.Linq;
using System.Data.Linq;

namespace QLDVu.BO
{
    class ChiTietPhieuBaoGiaBO
    {
        public static Table<HANGHOA> DanhSachHangHoa()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<HANGHOA>();

        }
        public static HANGHOA DanhSachHangHoa(string mahanghoa)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<HANGHOA> xe = dulieu.GetTable<HANGHOA>();
            HANGHOA x = xe.Single(xx => xx.MaHangHoa.Equals(mahanghoa));
            return x;

        }
        public static int DonGiaHangHoa(string mahanghoa)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
             //Table<CHITIETPHIEUNHAPKHO> chitiet = dulieu.GetTable<CHITIETPHIEUNHAPKHO>();
             //CHITIETPHIEUNHAPKHO s = chitiet.Single(ct => ct.MaHangHoa.Equals(mahanghoa));
             CHITIETPHIEUNHAPKHO s = dulieu.CHITIETPHIEUNHAPKHOs.Where(ct => ct.MaHangHoa.Equals(mahanghoa)).OrderByDescending(ct => ct.MaPhieuNhapKho).FirstOrDefault();
             if (s == null)
                 return 0;
            else
                return Convert.ToInt32(s.DonGia);
        }
        public static BAOGIA MaBaoGiaCuoiCung()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            BAOGIA s = dulieu.BAOGIAs.OrderByDescending(hh => hh.MaBaoGia).FirstOrDefault();
            return s;

        }
        public static CHITIETBAOGIA MaChiTietBaoGiaCuoiCung()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            CHITIETBAOGIA s = dulieu.CHITIETBAOGIAs.OrderByDescending(hh => hh.MaChiTietBaoGia).FirstOrDefault();
            return s;

        }
      
        
     
        public static Table<DICHVU> DanhSachDichVu()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<DICHVU>();

        }
        //public static Table<NHOMHANGHOA> DanhSachNhomHangHoa()
        //{
        //    QuanLyGaraDataContext dulieu = new QuanLyGaraDataContext();
        //    return dulieu.GetTable<NHOMHANGHOA>();

        //}
        public static DICHVU DanhSachDichVu(string madichvu)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<DICHVU> xe = dulieu.GetTable<DICHVU>();
            DICHVU x = xe.Single(xx => xx.MaDichVu.Equals(madichvu));
            return x;

        }

        public static IEnumerable<DICHVU> LayDichVuTheoLoaiDichVu(string maloaidichvu)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<DICHVU> dichvu = dulieu.GetTable<DICHVU>();
            IEnumerable<DICHVU> dv = from dvu in dichvu
                                      where dvu.MaLoaiDichVu.Equals(maloaidichvu)
                                     select dvu;
            return dv;


        }
        public static IEnumerable<BAOGIA> LayPhieuBaoGiaTheoSoXe(string biensoxe)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> baogia = dulieu.GetTable<BAOGIA>();
            IEnumerable<BAOGIA> bg = from bgia in baogia
                                     where bgia.BienSoXe.Equals(biensoxe)
                                     select bgia;
            return bg;


        }
        public static Table<LOAIDICHVU> DanhSachLoaiDichVu()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<LOAIDICHVU>();

        }
        public static void ThemPhieuBaoGia(string mabaogia,string makhachhang,string biensoxe,string manguoidung,string ngaylap,string tongtien)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> baogia = dulieu.GetTable<BAOGIA>();
            BAOGIA bg = new BAOGIA();
            bg.MaBaoGia = mabaogia;
            bg.NgayLap = Convert.ToDateTime(ngaylap);
            bg.MaKhachHang = makhachhang;
            bg.BienSoXe = biensoxe;
            bg.MaNguoiDung = manguoidung;
            bg.TongTien =Convert.ToInt32(tongtien);
            baogia.InsertOnSubmit(bg);
            dulieu.SubmitChanges();
        }
        public static void ThemChiTietPhieuBaoGia(string machitietbaogia,string mabaogia,string madichvu,string mahanghoa
            ,string dongia,string soluong,string thanhtien)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<CHITIETBAOGIA> chitietbaogia = dulieu.GetTable<CHITIETBAOGIA>();
            CHITIETBAOGIA ctbg = new CHITIETBAOGIA();
            ctbg.MaChiTietBaoGia = machitietbaogia;
            ctbg.MaBaoGia = mabaogia;
            ctbg.MaDichVu = madichvu;
            ctbg.MaHangHoa = mahanghoa;
            ctbg.DonGia = Convert.ToInt32(dongia);
            ctbg.SoLuong =Convert.ToInt32(soluong);
            ctbg.ThanhTien = Convert.ToInt32(thanhtien);
          
            chitietbaogia.InsertOnSubmit(ctbg);
            dulieu.SubmitChanges();
        }
       
        public static void XoaChiTietPhieuBaoGia(string maphieubaogia)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<CHITIETBAOGIA> chitietphieubaogias = dulieu.GetTable<CHITIETBAOGIA>();
            var dschitietpbg = from ct in chitietphieubaogias
                               where ct.MaBaoGia.Equals(maphieubaogia)
                               select ct;
            chitietphieubaogias.DeleteAllOnSubmit(dschitietpbg);
            dulieu.SubmitChanges();
        }

            public static void CapNhatPhieuBaoGia(string maphieubaogia,string makhachhang,string biensoxe,string manguoidung,string ngaylap,string tongtien)
            {
                QLDNDataContext dulieu = new QLDNDataContext();
                Table<BAOGIA> phieubaogias=dulieu.GetTable<BAOGIA>();
                BAOGIA pbg = phieubaogias.Single(pbgs => pbgs.MaBaoGia.Equals(maphieubaogia));
                pbg.MaKhachHang = makhachhang;
                pbg.BienSoXe = biensoxe;
                pbg.MaNguoiDung = manguoidung;
                pbg.NgayLap = Convert.ToDateTime(ngaylap);
                pbg.TongTien =Convert.ToInt32(tongtien);
                dulieu.SubmitChanges();
            }
        public static void XoaPhieuBaoGia(string maphieubaogia)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> phieubaogias = dulieu.GetTable<BAOGIA>();
            BAOGIA pbg = phieubaogias.Single(pbgs => pbgs.MaBaoGia.Equals(maphieubaogia));
            phieubaogias.DeleteOnSubmit(pbg);
            dulieu.SubmitChanges();
        }
        public static IEnumerable<CHITIETBAOGIA> LayChiTietPhieuBaoGia(string maphieubaogia)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<CHITIETBAOGIA> chitietphieubaogia = dulieu.GetTable<CHITIETBAOGIA>();
            IEnumerable<CHITIETBAOGIA> ct = from chitiet in chitietphieubaogia
                                            where chitiet.MaBaoGia.Equals(maphieubaogia)
                                            select chitiet;
            return ct;
        }
        public static Table<BAOGIA> DanhSachPhieuBaoGia()
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            return dulieu.GetTable<BAOGIA>();
        }
        public static BAOGIA PhieuBaoGiaTheoMa(string mabaogia)
        {
            QLDNDataContext dulieu = new QLDNDataContext();
            Table<BAOGIA> phieubaogia= dulieu.GetTable<BAOGIA>();
            BAOGIA bg = phieubaogia.Single(pbg => pbg.MaBaoGia.Equals(mabaogia));
            return bg;
        }

    }
}
